package com.tanhua.server.controller;

import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author げっしょく
 * @date 2021/10/26
 */
@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    /**
     * 发布动态
     * @param movement 发布的文字，位置，
     * @param imageContent 发布的图片
     * @return
     */
    @PostMapping
    public ResponseEntity publishmovements(Movement movement, MultipartFile[] imageContent) throws IOException {
        movementsService.publishmovement(movement, imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 分页获取指定用户的所以动态
     * @param page 页码
     * @param pagesize 条数
     * @param userId 用户
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity allmovements(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize,
                                       Long userId) {
        PageResult pageResult = movementsService.PgaeMovements(page, pagesize, userId);
        return ResponseEntity.ok(pageResult);

    }

    /**
     * 分页查询好友动态
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity getFriendMovements(@RequestParam(defaultValue = "1") Integer page,
                                             @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementsService.getFriendMovements(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 查询推荐动态
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = movementsService.recommend(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询单条动态
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity getById(@PathVariable String id) {
        MovementsVo movementsVo = movementsService.getById(id);
        return ResponseEntity.ok(movementsVo);
    }


    /**
     * 动态点赞
     * @param id
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable String id) {
        Integer integer = movementsService.like(id, CommentType.LIKE, true);
        return ResponseEntity.ok(integer);
    }

    /**
     * 动态取消点赞
     * @param id
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable String id) {
        Integer integer = movementsService.like(id, CommentType.LIKE, false);
        return ResponseEntity.ok(integer);
    }


    /**
     * 动态喜欢
     * @param id
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable String id) {
        Integer integer = movementsService.like(id, CommentType.LOVE, true);
        return ResponseEntity.ok(integer);
    }

    /**
     * 动态取消喜欢
     * @param id
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable String id) {
        Integer integer = movementsService.like(id, CommentType.LOVE, false);
        return ResponseEntity.ok(integer);
    }

    //查看谁看过我的资料详情
    @GetMapping("/visitors")
    public ResponseEntity visitors() {
        List<VisitorsVo> list = movementsService.visitors();
        return ResponseEntity.ok(list);
    }

}
